<html>
<head><meta charset="utf-8"><title>Telling apart `quote_spanned` from non-macro  spans? · t-compiler/wg-diagnostics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/index.html">t-compiler/wg-diagnostics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html">Telling apart `quote_spanned` from non-macro  spans?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222400188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222400188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Phil H <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222400188">(Jan 12 2021 at 07:50)</a>:</h4>
<p>Hi <span aria-label="wave" class="emoji emoji-1f44b" role="img" title="wave">:wave:</span> <br>
In Clippy, we're running into cases where code generated by <code>quote_spanned</code> triggers various lints and it's not clear whether there's a way to avoid this at all.<br>
Here's an example <code>proc_macro_attribute</code> where code in <code>quote_spanned</code> triggers a Clippy lint:<br>
<a href="https://github.com/rust-lang/rust-clippy/issues/6514#issuecomment-758279802">https://github.com/rust-lang/rust-clippy/issues/6514#issuecomment-758279802</a><br>
It seems that this is potentially also an issue with rustc lints: <a href="https://github.com/rust-lang/rust/issues/78862">https://github.com/rust-lang/rust/issues/78862</a></p>
<p>I guess I'm asking, if there is any way to tell apart <code>quote_spanned</code> spans from non-macro spans?</p>



<a name="222415249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222415249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222415249">(Jan 12 2021 at 10:50)</a>:</h4>
<p>IIRC you are expected to add an attribute on macro generated code so that it doesn't lint. I don't remember what it was off hand tho</p>



<a name="222426511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222426511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222426511">(Jan 12 2021 at 13:02)</a>:</h4>
<p>Hm, I thought we had machinery to detect if a span was a result of macro expansion - regardless of usage of quote_spanned</p>



<a name="222427425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222427425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222427425">(Jan 12 2021 at 13:10)</a>:</h4>
<p>We do, but that relies on the span being produced by macro rules expansion to begin with</p>



<a name="222427450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222427450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222427450">(Jan 12 2021 at 13:10)</a>:</h4>
<p>a proc macro can use <code>quote_spanned</code> (or <code>set_span</code>) to associate arbitrary spans with arbitrary tokens</p>



<a name="222427501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222427501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222427501">(Jan 12 2021 at 13:11)</a>:</h4>
<p>if a proc-macro associated a normal user-provided span with tokens that it generates, we can't detect it as macro generated</p>



<a name="222427678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222427678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222427678">(Jan 12 2021 at 13:12)</a>:</h4>
<p>hm, interesting - I would expect for that to set the location but still associate the span with the macro's expansion id (if I'm remembering name right)</p>



<a name="222429156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222429156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222429156">(Jan 12 2021 at 13:21)</a>:</h4>
<p>there are stablizied apis to do any combination of those things</p>



<a name="222429272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222429272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222429272">(Jan 12 2021 at 13:22)</a>:</h4>
<p>Using <code>set_span</code> overwrites the entire <code>Span</code></p>



<a name="222429360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222429360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222429360">(Jan 12 2021 at 13:22)</a>:</h4>
<p>you can use <code>Span::located_at</code> and <code>Span::resolved_at</code> to set the location and hygiene (<code>SyntaxContext</code>) individually from other spans</p>



<a name="222430558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222430558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222430558">(Jan 12 2021 at 13:33)</a>:</h4>
<p>Hm ok</p>



<a name="222430728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222430728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222430728">(Jan 12 2021 at 13:35)</a>:</h4>
<p>In order for us to be able to reliably detect spans produced by macro expansion, we would need to store some extra state which is unaffected by the <code>proc_macro::Span</code> apis</p>



<a name="222431194"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/Telling%20apart%20%60quote_spanned%60%20from%20non-macro%20%20spans%3F/near/222431194" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/Telling.20apart.20.60quote_spanned.60.20from.20non-macro.20.20spans.3F.html#222431194">(Jan 12 2021 at 13:39)</a>:</h4>
<p>That makes sense, I guess I had assumed we had that already.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>